﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="report_score_request.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Class.report_score_request" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <%
        int id = int.Parse(Request.QueryString["id"]);
        var get_student = new TblUserController().FetchAll().Where(x => x.ClassX == id);
        int group = int.Parse(Request.QueryString["group"]);
        var get_exam = new TblExamController().FetchAll().Where(x => x.ExamGroup == group);
        int count = 0;
        var get_semester = new TblExamGroupController().FetchAll().Where(x => x.GroupId == get_exam.First().ExamGroup).First();
        if (get_student.ToList().Count != 0)
        { %>
    <table class="w3-white">
        <tr class="">
            <td class="name-col view-name-col w3-deep-purple">Họ tên</td>
            <td rowspan="<%=get_student.ToList().Count + 1 %>" class="middle-point-col">
                <div id="class-point-slide" class="w3-display-container carousel slide" data-ride="carousel">
                    <div class="carousel-inner" role="listbox">
                        <%foreach (TblExam exam in get_exam)
                            { %>
                        <div class="item <%if (count == 0)
                            {%>active<%} %> w3-center">
                            <div class="w3-orange w3-text-white w3-padding-4"><%=exam.ExamName %></div>
                            <%if (get_semester.Semester != 3)
                                { %>
                            <div class="w3-row">
                                <div class="w3-col point-col w3-blue">Từ vựng</div>
                                <div class="w3-col point-col w3-indigo">Ngữ pháp</div>
                                <div class="w3-col point-col w3-purple">Luyện nghe</div>
                                <div class="w3-col point-col w3-green">Hán tự</div>
                                <%--<div class="w3-col point-col w3-red">Giao tiếp</div>--%>
                            </div>
                            <%}
                                else
                                { %>
                            <div class="w3-row">
                                <div class="w3-col point-col w3-blue">Viết</div>
                                <div class="w3-col point-col w3-indigo">Phỏng vấn</div>
                                <div class="w3-col point-col w3-purple">KTNN</div>
                                <div class="w3-col point-col w3-green">Nghe</div>
                            </div>
                            <%} %>
                            <%foreach (TblUser student in get_student)
                                {
                                    float? write = null, grammar = null, listen = null, kanji = null, speak = null;
                                    var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == student.UserId && x.ScoreExam == exam.ExamId).ToList();
                                    if (get_score.Count != 0)
                                    {
                                        if (get_score[0].ScoreListen != null)
                                            listen = (float)get_score[0].ScoreListen;
                                        if (get_score[0].ScoreGrammar != null)
                                            grammar = (float)get_score[0].ScoreGrammar;
                                        if (get_score[0].ScoreKanji != null)
                                            kanji = (float)get_score[0].ScoreKanji;
                                        if (get_score[0].ScoreSpeak != null)
                                            speak = (float)get_score[0].ScoreSpeak;
                                        if (get_score[0].ScoreWrite != null)
                                            write = (float)get_score[0].ScoreWrite;
                                    }%>
                            <%if (get_semester.Semester != 3)
                                { %>
                            <div class="w3-row">
                                <%if (write != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=write %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (grammar != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=grammar %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (listen != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=listen %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (kanji != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=kanji %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%--<%if (speak != null)
                                {%>
                                <div class="w3-col point-col view-point-col"><%=speak %></div>
                                <%}
                                else
                                { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>--%>
                            </div>
                            <%}
                                else
                                { %>
                            <div class="w3-row">
                                <%if (write != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=write %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (speak != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=speak %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (kanji != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=kanji %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                                <%if (listen != null)
                                    {%>
                                <div class="w3-col point-col view-point-col"><%=listen %></div>
                                <%}
                                    else
                                    { %>
                                <div class="w3-col point-col view-point-col">&nbsp;</div>
                                <%} %>
                            </div>
                            <%}
                                } %>
                        </div>
                        <% count++;
                            } %>
                    </div>

                    <div class="class-point-slide-btn w3-display-topmiddle">
                        <a class="w3-text-white w3-left" href="#class-point-slide" role="button" data-slide="prev">
                            <i class="fa fa-chevron-left"></i>
                        </a>
                        <a class="w3-text-white w3-right" href="#class-point-slide" role="button" data-slide="next">
                            <i class="fa fa-chevron-right"></i>
                        </a>
                    </div>
                </div>
            </td>
            <td class="average-col view-average-col w3-light-blue">Trung bình</td>
        </tr>
        <%foreach (TblUser student in get_student)
            {
                decimal? average_exam = 0, average_total = 0;
                foreach (TblExam exam in get_exam)
                {
                    var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == student.UserId && x.ScoreExam == exam.ExamId);
                    if (get_score.ToList().Count == 0)
                    {
                        average_exam = null;
                        average_total = null;
                        break;
                    }
                    else
                    {
                        if (get_semester.Semester != 3)
                        {
                            if (get_score.Single().ScoreWrite == null || get_score.Single().ScoreGrammar == null || get_score.Single().ScoreListen == null || get_score.Single().ScoreKanji == null)
                            {
                                average_exam = null;
                                average_total = null;
                                break;
                            }
                            else
                            {
                                decimal? temp = get_score.Single().ScoreWrite + get_score.Single().ScoreGrammar + get_score.Single().ScoreListen + get_score.Single().ScoreKanji;

                                if (exam.ExamTotal == true)
                                    average_total += temp / (decimal)4 * (decimal)2;
                                else
                                    average_exam += temp;
                            }
                        }
                        else
                        {
                            if (get_score.Single().ScoreWrite == null || get_score.Single().ScoreListen == null || get_score.Single().ScoreKanji == null || get_score.Single().ScoreSpeak == null)
                            {
                                average_exam = null;
                                average_total = null;
                                break;
                            }
                            else
                            {
                                decimal? temp = get_score.Single().ScoreWrite + get_score.Single().ScoreListen + get_score.Single().ScoreKanji + get_score.Single().ScoreSpeak;

                                if (exam.ExamTotal == true)
                                    average_total += temp * (decimal)2;
                                else
                                    average_exam += temp;
                            }
                        }
                    }
                }
        %>
        <tr>
            <td class="name-col"><%=student.FullName %></td>
            <%if (average_exam != null && average_total != null)
                {
                    decimal? average = 0;
                    var count_exam = new TblExamController().FetchAll().Where(x => x.ExamGroup == group && x.ExamTotal == false);
                    int count_temp = count_exam.ToList().Count * 4;
                    if (get_semester.Semester != 3)
                        average = (average_exam / decimal.Parse(count_temp.ToString()) + average_total) / decimal.Parse("3");
                    else
                        average = average_exam / decimal.Parse(count_temp.ToString());
            %>
            <td class="average-col"><%=Math.Round(decimal.Parse(average.ToString()), 2) %></td>
            <%}
                else
                { %>
            <td class="average-col">&nbsp;</td>
            <%} %>
        </tr>
        <%} %>
    </table>
    <%} %>
</body>
</html>
